package com.room.search.service;

import com.room.search.core.page.TableDataInfo;
import com.room.search.domain.RoomListingFeature;
import com.room.search.repository.RoomListingFeatureRepository;
import com.room.search.response.RoomResult;
import com.room.search.service.dto.RoomListingFeatureDTO;
import com.room.search.service.mapper.RoomListingFeatureMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.room.search.util.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link RoomListingFeature}.
 */
@Service
@Transactional
public class RoomListingFeatureService {

    private final Logger log = LoggerFactory.getLogger(RoomListingFeatureService.class);

    private final RoomListingFeatureRepository roomListingFeatureRepository;

    private final RoomListingFeatureMapper roomListingFeatureMapper;

    public RoomListingFeatureService(
        RoomListingFeatureRepository roomListingFeatureRepository,
        RoomListingFeatureMapper roomListingFeatureMapper
    ) {
        this.roomListingFeatureRepository = roomListingFeatureRepository;
        this.roomListingFeatureMapper = roomListingFeatureMapper;
    }

    /**
     * Save a roomListingFeature.
     *
     * @param roomListingFeatureDTO the entity to save.
     * @return the persisted entity.
     */
    public RoomListingFeatureDTO save(RoomListingFeatureDTO roomListingFeatureDTO) {
        log.debug("Request to save RoomListingFeature : {}", roomListingFeatureDTO);
        RoomListingFeature roomListingFeature = roomListingFeatureMapper.toEntity(roomListingFeatureDTO);
        roomListingFeature = roomListingFeatureRepository.save(roomListingFeature);
        return roomListingFeatureMapper.toDto(roomListingFeature);
    }

    /**
     * Partially update a roomListingFeature.
     *
     * @param roomListingFeatureDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<RoomListingFeatureDTO> partialUpdate(RoomListingFeatureDTO roomListingFeatureDTO) {
        log.debug("Request to partially update RoomListingFeature : {}", roomListingFeatureDTO);

        return roomListingFeatureRepository
            .findById(roomListingFeatureDTO.getId())
            .map(existingRoomListingFeature -> {
                roomListingFeatureMapper.partialUpdate(existingRoomListingFeature, roomListingFeatureDTO);

                return existingRoomListingFeature;
            })
            .map(roomListingFeatureRepository::save)
            .map(roomListingFeatureMapper::toDto);
    }

    /**
     * Get all the roomListingFeatures.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<RoomListingFeatureDTO> findAll() {
        log.debug("Request to get all RoomListingFeatures");
        return roomListingFeatureRepository
            .findAll()
            .stream()
            .map(roomListingFeatureMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one roomListingFeature by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<RoomListingFeatureDTO> findOne(Long id) {
        log.debug("Request to get RoomListingFeature : {}", id);
        return roomListingFeatureRepository.findById(id).map(roomListingFeatureMapper::toDto);
    }

    /**
     * Delete the roomListingFeature by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete RoomListingFeature : {}", id);
        roomListingFeatureRepository.deleteById(id);
    }

    /**
     * 获取所有房源特色
     */
    public TableDataInfo getAllFeature() {
        Pageable pageable = PageUtils.startPage();
        Page<RoomListingFeature> all = roomListingFeatureRepository.findAll(pageable);
        return TableDataInfo.getTableDataInfo(all);
    }

    /**
     * 添加房源特色
     * @param dto 房源特色Dto
     */
    public RoomResult addFeature(RoomListingFeatureDTO dto) {
        RoomListingFeature roomListingFeature = roomListingFeatureRepository.findByFeatureName(dto.getFeatureName()).orElse(null);
        if (roomListingFeature != null) {
            return RoomResult.error("已经有该房源特色存在");
        }
        roomListingFeature = roomListingFeatureMapper.toEntity(dto);
        roomListingFeatureRepository.save(roomListingFeature);
        return RoomResult.success("添加房源特色成功");
    }


    public RoomResult getFeatureById(Long id) {
        RoomResult success = RoomResult.success();
        RoomListingFeature feature = roomListingFeatureRepository.findById(id).orElse(null);
        success.put("data", feature);
        return success;
    }

    public RoomResult editFeature(RoomListingFeatureDTO dto) {
        return roomListingFeatureRepository.findById(dto.getId())
            .map(roomListingFeature -> {
                if (roomListingFeature.getFeatureName().equals(dto.getFeatureName())) {
                    return RoomResult.error("存在该房源特色");
                }
                roomListingFeature.setFeatureName(dto.getFeatureName());
                roomListingFeatureRepository.save(roomListingFeature);
                return RoomResult.success("修改成功");
            }).orElseGet(
            ()-> RoomResult.error("不存该房源特色")
        );
    }
}
